/*global jQuery, window, Modernizr, navigator*/

(function ($, win, Modernizr, nav, doc) {
  "use strict";
  $(function () {
    /* ---------------------------------------------------------------------- */
    /*	Detect Touch Device													  */
    /* ---------------------------------------------------------------------- */

    (function () {
      if (Modernizr.touch) {
        $('body').addClass('touch-device');
      }
      if ($.browser.safari === true) {
        $('body').addClass('safari');
      }
    }());

    /* end Detect Touch Device */

    /*----------------------------------------------------*/
    /*	Alert Boxes										  */
    /*----------------------------------------------------*/

    (function () {
      var $notifications = $('.error, .success, .info, .notice, .status');
      if ($notifications.length) {
        $notifications.notifications({
          speed: 300
        });
      }
    }());

    /* ---------------------------------------------------- */
    /* Back to Top											*/
    /* ---------------------------------------------------- */

    (function () {
      var extend = {
        button: '#back-top',
        text: 'Back to Top',
        min: 200,
        fadeIn: 400,
        fadeOut: 400,
        speed: 800
      }, oldiOS = false, oldAndroid = false;

      // Detect if older iOS device, which doesn't support fixed position
      if (/(iPhone|iPod|iPad)\sOS\s[0-4][_\d]+/i.test(nav.userAgent)) {
        oldiOS = true;
      }

      // Detect if older Android device, which doesn't support fixed position
      if (/Android\s+([0-2][\.\d]+)/i.test(nav.userAgent)) {
        oldAndroid = true;
      }

      $('body').append('<a href="#" id="' + extend.button.substring(1) + '" title="' + extend.text + '">' + extend.text + '</a>');

      $(win).scroll(function () {
        var pos = $(win).scrollTop();

        if (oldiOS || oldAndroid) {
          $(extend.button).css({
            'position': 'absolute',
            'top': pos + $(win).height()
          });
        }

        if (pos > extend.min) {
          $(extend.button).fadeIn(extend.fadeIn);
        } else {
          $(extend.button).fadeOut(extend.fadeOut);
        }

      });

      $(extend.button).bind('click', function (e) {
        $('html, body').animate({
          scrollTop: 0
        }, extend.speed);
        e.preventDefault();
      });

    }());

    /* end Back to Top */
    /* ---------------------------------------------------- */
    /*	Preloader											*/
    /* ---------------------------------------------------- */

    (function () {

      $.preloader = function (el, options) {
        var elem = $(el), methods = {},
          elements = elem.find('.preloader'),
          o = $.extend({}, $.preloader.defaults, options);

        methods = {
          init: function () {
            this.loader();
            this.eventListener();
          },
          eventListener: function () {
            $(win).load(function () {
              elements.each(function (i, val) {
                $(val).removeClass('loader');
              });
            });
          },
          loader: function () {
            elements.each(function (i, val) {
              win.setTimeout(function () {
                $(val).addClass('loader');
              }, i * o.speed);
            });
          }
        };
        methods.init();

      };

      $.preloader.defaults = {speed : 250};

      $.fn.preloader = function (options) {
        if (typeof options === 'object') {
          return this.each(function () {
            new $.preloader(this, options);
          });
        };
      };

      $('.container').preloader({
        speed: 300
      });

    }());

    /* end Preloader */
  });
  /* ---------------------------------------------------- */
  /*	Notifications										*/
  /* ---------------------------------------------------- */

  $.fn.notifications = function (options) {
    var defaults = {speed: 200}, o = $.extend({}, defaults, options);
    return this.each(function () {
      var closeBtn = $('<a class="alert-close" href="#">' + Drupal.t('Close') + '</a>'),
        closeButton = $(this).append(closeBtn).find('> .alert-close');
      function fadeItSlideIt(object) {
        object.fadeTo(o.speed, 0, function () {
          object.slideUp(o.speed);
        });
      }
      closeButton.click(function () {
        fadeItSlideIt($(this).parent());
        return false;
      });
    });
  };

  /* end jQuery Notifications */

  /* ---------------------------------------------------- */
  /*	Actual Plugin										*/
  /* ---------------------------------------------------- */

  // jQuery Actual Plugin - Version: 1.0.13 (http://dreamerslab.com/)
  ;(function(a){a.fn.extend({actual:function(b,l){if(!this[b]){throw'$.actual => The jQuery method "'+b+'" you called does not exist';}var f={absolute:false,clone:false,includeMargin:false};var i=a.extend(f,l);var e=this.eq(0);var h,j;if(i.clone===true){h=function(){var m="position: absolute !important; top: -1000 !important; ";e=e.clone().attr("style",m).appendTo("body");};j=function(){e.remove();};}else{var g=[];var d="";var c;h=function(){c=e.parents().andSelf().filter(":hidden");d+="visibility: hidden !important; display: block !important; ";if(i.absolute===true){d+="position: absolute !important; ";}c.each(function(){var m=a(this);g.push(m.attr("style"));m.attr("style",d);});};j=function(){c.each(function(m){var o=a(this);var n=g[m];if(n===undefined){o.removeAttr("style");}else{o.attr("style",n);}});};}h();var k=/(outer)/g.test(b)?e[b](i.includeMargin):e[b]();j();return k;}});})(jQuery);

  /* end jQuery Actual Plugin */

}(jQuery, window, Modernizr, navigator, document));